<?php
/*  
  Copyright (c) 2010-02 SongCMS 
  SongCMS All Rights Reserved. 
  Support:www.SongCMS.com
  QQ:544255858   19951025
  Author:Song  Version:3.17
  Date:2010-08-12 09:28:32
*/
//数组 $customize_field 分别为：0字段名称，1标识，2类型，3大小，4默认值
include_once('Admin.php');
include_once('../config/field.php');
if(empty($_GET['t'])) $_GET['t'] = 3;
$sTitle = '添加自定义字段'; $sub = ' 添 加 ';
$table = $_GET['t'] == 2 ? "{$dbprefix}news" : ($_GET['t'] == 3 ? "{$dbprefix}article" : "{$dbprefix}product"); //当前表名
$fields = $_GET['t'] == 2 ? $customize_field['news'] : ($_GET['t'] == 3 ? $customize_field['page'] : $customize_field['product']); //当前类型 array
$a_name = $_GET['t'] == 2 ? 'news' : ($_GET['t'] == 3 ? 'page' : 'product'); //数组中键名
if(!is_writable('../config/field.php')) die('无权写入配置文件：../config/field.php ,请开通该文件可写权限, 否则无法正常使用该功能。');	

//修改时默认数据
$field_name = $field_text = $field_type = $field_size = $field_defa = NULL; 
if($_GET['a'] == 'c'){
	if(!is_numeric($_GET['id'])) MsgBox (1,'id 参数错误。','-1');
	$sTitle = '修改字段'; $sub = ' 修 改 ';
	foreach($fields as $i => $n){
		if($i == $_GET['id']){
			$field_name = $n[0];
			$field_text = $n[1];
			$field_type = $n[2];
			$field_size = $n[3];
			$field_defa = $n[4];
		}
	}
}

//增加、修改 
if(!empty($_POST['field_name'])){
	$field_type = $_POST['field_type']=='IMGVARCHAR' ? 'VARCHAR' : trim($_POST['field_type']);
	$_POST['field_size'] = $_POST['field_type']=='IMGVARCHAR' ? 255 : trim($_POST['field_size']);
	
	if(@!ereg("^[0-9a-zA-Z\_]*$",$_POST['field_name'])) MsgBox (1,'非法的字段名称。','1'); 
	if($field_type == 'TEXT' || $field_type == 'MEDIUMTEXT' || $field_type == 'LONGTEXT' || $field_type == 'DATE' || $field_type == 'DATETIME'){
		$SQL_add = "ALTER TABLE `{$table}` ADD `{$_POST['field_name']}` {$field_type} NULL";
		$SQL_update = "ALTER TABLE `{$table}` CHANGE `{$field_name}` `{$_POST['field_name']}` {$field_type} NULL";
	} else {
		$DEFAULT = trim($_POST['field_defa']) == '' ? '' : "DEFAULT '{$_POST['field_defa']}'";
		$SQL_add = "ALTER TABLE `{$table}` ADD `{$_POST['field_name']}` {$field_type}({$_POST['field_size']}) NULL {$DEFAULT}";
		$SQL_update = "ALTER TABLE `{$table}` CHANGE `{$field_name}` `{$_POST['field_name']}` {$field_type}({$_POST['field_size']}) NULL {$DEFAULT}";
	}
	
	if($_POST['Action'] == 'add'){
		$SQL = $SQL_add;
		$customize_field[$a_name][] = array(trim($_POST['field_name']),trim($_POST['field_text']),trim($_POST['field_type']),trim($_POST['field_size']),trim($_POST['field_defa']));
		$ntext = "添加自定义字段";
		$nx = 2;
	} else {
		$SQL = $SQL_update;
		foreach($customize_field[$a_name] as $i => &$n){
			if($i == $_GET['id']){
				$n[0] = trim($_POST['field_name']);
				$n[1] = trim($_POST['field_text']);
				$n[2] = trim($_POST['field_type']);
				$n[3] = trim($_POST['field_size']);
				$n[4] = trim($_POST['field_defa']);
			}
		}
		$ntext = "修改自定义字段";
		$nx = 1;
	}
	
	if($db -> ExecuteSQL($SQL)){
		$fieldtext = '<?php'."\r\n".'$customize_field = '.var_export($customize_field,true).";\r\n?>";
		if(file_put_contents('../config/field.php',$fieldtext)){
			AdminLog($nx,$Lang,"{$ntext}(表:{$table})成功。"); 
			MsgBox (1,"{$ntext}成功。","CustomizeField.php?t={$_GET['t']}");
		} else {
			if($_POST['Action'] == 'add') $db -> ExecuteSQL("ALTER TABLE `{$table}` DROP `{$_POST['field_name']}`"); 
			AdminLog($nx,$Lang,"{$ntext}(表:{$table})失败，无法写入配置文件。"); 
			MsgBox (1,"{$ntext}失败！无法写入自定义配置信息：../config/field.php",'-1');
		}			
	}else{
		AdminLog($nx,$Lang,"{$ntext}(表:{$table})失败。"); 
		MsgBox (1,$ntext.'失败！可能有以下原因：\r\n1、字段已经存在或字段名不规范。\r\n2、填写的字段长度与选择的字段类型不符。\r\n3、字符默认值与您选择的字段类型不符。','-1');
	}
}

//删除
if($_GET['a'] == 'd' && is_numeric($_GET['id']) && !empty($_GET['fn'])){
	unset($customize_field[$a_name][$_GET['id']]);
	$customize_field[$a_name] = array_values($customize_field[$a_name]); //重新索引
	$fieldtext = '<?php'."\r\n".'$customize_field = '.var_export($customize_field,true).";\r\n?>";
	if(file_put_contents('../config/field.php',$fieldtext)){
		$SQL = "ALTER TABLE `{$table}` DROP `{$_GET['fn']}`";
		if($db -> ExecuteSQL($SQL)){
			AdminLog(3,$Lang,"删除表({$table})中的字段({$_GET['fn']})成功。"); 
			MsgBox (0,"","CustomizeField.php?t={$_GET['t']}");			
		} else {
			AdminLog(3,$Lang,"删除表({$table})中的字段({$_GET['fn']})失败，写入配置文件成功。"); 
			MsgBox (0,"","CustomizeField.php?t={$_GET['t']}");				
		}
	} else {
		AdminLog(3,$Lang,"删除表({$table})中的字段({$_GET['fn']})失败，无法写入配置文件：../config/field.php"); 
		MsgBox (1,"删除字段失败，无法写入配置文件：../config/field.php",'-1');		
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="./images/adminstyle.css" rel="stylesheet" type="text/css" />
<link rel="Shortcut Icon" href="../favicon.ico">
<link rel="Bookmark" href="../favicon.ico">
<script src="../js/csshovernotie6.js" type="text/javascript"></script>
</head>
<body>
<br />
<table width="98%" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
  <form id="form1" name="form1" method="post" action="CustomizeField.php?<?php echo "t={$_GET['t']}&a={$_GET['a']}&id={$_GET['id']}";?>">
  <tr>
    <td colspan="3" align="center" class="Main_Title"><?php echo $sTitle;?></td>
  </tr>
  <tr class="odd">
  	<td height="20" align="right">数据表：</td>
  	<td>
		<select onchange="window.open(this.options[this.selectedIndex].value,'_self')" <?php if($_GET['a'] == 'c') echo 'disabled="disabled"';?> size="1"> 
		<option <?php if($_GET['t'] == 1) echo 'selected="selected"';?> value="CustomizeField.php?t=1">产品表 (<?php echo $dbprefix;?>product)</option> 
		<option <?php if($_GET['t'] == 2) echo 'selected="selected"';?> value="CustomizeField.php?t=2">新闻表 (<?php echo $dbprefix;?>news)</option> 
		<option <?php if($_GET['t'] == 3) echo 'selected="selected"';?> value="CustomizeField.php?t=3">栏目表 (<?php echo $dbprefix;?>article)</option> 
		</select>		
	</td>
  	<td>请选择要添加的数据表</td>
  	</tr>
  <tr class="odd">
    <td height="20" align="right">字段名称：</td>
    <td>
    	<input name="field_name" type="text" id="field_name" maxlength="30" value="<?php echo $field_name;?>" />
    </td>
    <td>由英文或数字组成，且不能以数字开头。如："title"</td>
  </tr>
  <tr class="odd">
  	<td height="20" align="right">字段标识：</td>
  	<td><input name="field_text" type="text" maxlength="50" value="<?php echo $field_text;?>" /></td>
  	<td>如：标题。在后台添加和修改数据显示</td>
  	</tr>
  <tr class="odd">
  	<td height="20" align="right">字段类型：</td>
  	<td>
		<select name="field_type" id="select"> 
          <?php if($_GET['t'] == 3){?><option <?php if($field_type == 'IMGVARCHAR') echo 'selected="selected"';?> value="IMGVARCHAR">图片字段,带上传功能 (VARCHAR)</option><?php }?>
          <option <?php if($field_type == 'VARCHAR') echo 'selected="selected"';?> value="VARCHAR">字符型0-255字节 (VARCHAR)</option> 
          <option <?php if($field_type == 'TEXT') echo 'selected="selected"';?> value="TEXT">小型字符型 (TEXT)</option> 
          <option <?php if($field_type == 'MEDIUMTEXT') echo 'selected="selected"';?> value="MEDIUMTEXT">中型字符型 (MEDIUMTEXT)</option> 
          <option <?php if($field_type == 'LONGTEXT') echo 'selected="selected"';?> value="LONGTEXT">大型字符型 (LONGTEXT)</option> 
          <option <?php if($field_type == 'TINYINT') echo 'selected="selected"';?> value="TINYINT">小数值型 (TINYINT)</option> 
          <option <?php if($field_type == 'SMALLINT') echo 'selected="selected"';?> value="SMALLINT">中数值型 (SMALLINT)</option> 
          <option <?php if($field_type == 'INT') echo 'selected="selected"';?> value="INT">大数值型 (INT)</option> 
          <option <?php if($field_type == 'BIGINT') echo 'selected="selected"';?> value="BIGINT">超大数值型 (BIGINT)</option> 
          <option <?php if($field_type == 'FLOAT') echo 'selected="selected"';?> value="FLOAT">数值浮点型 (FLOAT)</option> 
          <option <?php if($field_type == 'DOUBLE') echo 'selected="selected"';?> value="DOUBLE">数值双精度型 (DOUBLE)</option> 
          <option <?php if($field_type == 'DATE') echo 'selected="selected"';?> value="DATE">日期型 (DATE)</option> 
          <option <?php if($field_type == 'DATETIME') echo 'selected="selected"';?> value="DATETIME">日期时间型 (DATETIME)</option> 
        </select> 	
	</td>
  	<td>只有栏目表中可以添加带有上传功能的图片字段</td>
  	</tr>
	<tr class="odd">
  	<td height="20" align="right">长度：</td>
  	<td><input type="text" name="field_size" id="field_size" value="<?php echo $field_size;?>" /></td>
  	<td>根据选择的字段类型填写合适的长度</td>
  	</tr>
  <tr class="odd">
  	<td height="20" align="right">默认值：</td>
  	<td><input type="text" name="field_defa" id="field_defa" value="<?php echo $field_defa;?>" /></td>
  	<td>如：0 或 NULL</td>
  	</tr>
  <tr class="odd">
  	<td height="20" align="right">&nbsp;</td>
  	<td>
		<input type="hidden" name="Action" value="<?php if($_GET['a'] == 'c'){echo 'update';} else {echo 'add';}?>" />
		<input type="submit" name="button" id="button" value="<?php echo $sub;?>" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<?php if($_GET['a'] == 'c') echo "<input type=\"button\" value=\" 返 回 \" onClick=\"javascript:location.href='CustomizeField.php?t={$_GET['t']}'\" />";?>
	</td>
  	<td>&nbsp;</td>
  	</tr>
	</form>
</table>
<br />
<table width="98%" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
  <tr>
    <td colspan="5" align="center" class="Main_Title">自定义字段管理</td>
  </tr>
  <tr class="odd">
  	<td align="center"><strong>字段名称</strong></td>
  	<td align="center"><strong>字段标识</strong></td>
    <td height="20" align="center"><strong>字段类型(长度)</strong></td>
    <td align="center"><strong>默认值</strong></td>
    <td align="center"><strong>管理选项</strong></td>
  </tr>
<?php
foreach($fields as $i => &$n){
	if($n[2] == 'IMGVARCHAR') $n[2] = '图片上传';
	$xtype = empty($n[3]) ? $n[2] : "{$n[2]} ({$n[3]})";
	echo "<tr class=\"odd\">
  	<td align=\"center\" height=\"20\">{$n[0]}</td>
  	<td align=\"center\">{$n[1]}</td>
    <td align=\"center\">{$xtype}</td>
    <td align=\"center\">{$n[4]}</td>
    <td align=\"center\"><a href=\"CustomizeField.php?a=c&t={$_GET['t']}&id={$i}\">修改字段</a> | <a href=\"CustomizeField.php?a=d&t={$_GET['t']}&id={$i}&fn={$n[0]}\" onClick=\"return confirm('确定要删除这个字段吗？该操作不可逆，请谨慎操作。');\">删除字段</a></td>
 	</tr>";	
}
if(empty($fields)) echo '<tr class="odd"><td height="20" colspan="5" align="center">没有自定义字段</td></tr>';
?>  
</table>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="6">
	<tr class="Notodd">
		<td style="color:#333333; line-height:20px;">
		注：1、添加后的自定义字段会自动在后台相关的发布及修改页面显示。<br />
		　　2、前台调用根据您的模板变量而定。如在产品表中新增一个字段名为title的字段，在产品内页(product.html)调用：&lt;!--{$p.title}--&gt;</td>
	</tr>
</table>  
</body>
</html>
